#include "can.h"

int can::opendev(const char *name)
{
    msockfd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (msockfd < 0) {
        perror("socket");
        return -1;
    }

    strncpy(mifreq.ifr_name, name, strlen(name)+1);
    mifreq.ifr_ifindex = if_nametoindex(mifreq.ifr_name);
    if (!mifreq.ifr_ifindex) {
        perror("if_nametoindex");
        return -1;
    }

    maddr.can_family = AF_CAN;
    maddr.can_ifindex = mifreq.ifr_ifindex;
    if (bind(msockfd, (struct sockaddr *)&maddr, sizeof(maddr)) < 0) {
        perror("bind");
        return -1;
    }

    return 0;
}










